<template>
  <div class="app-container" v-if="!isForm">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="年度组号" prop="yearGroup">
        <el-input
          v-model="queryParams.yearGroup"
          placeholder="请输入年度组号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="检查事项" prop="applyReason">
        <el-input
          v-model="queryParams.applyReason"
          placeholder="请输入检查事项"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查企业" prop="vendorName">
        <el-input
          v-model="queryParams.vendorName"
          placeholder="请输入检查企业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查地点" prop="checkAddress">
        <el-input
          v-model="queryParams.checkAddress"
          placeholder="请输入检查地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查范围" prop="checkRange">
        <el-input
          v-model="queryParams.checkRange"
          placeholder="请输入检查范围"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select clearable v-model="queryParams.status">
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="creatDate">
        <el-date-picker
          v-model="queryParams.creatDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          v-hasPermi="['collect:tapRecord:edit']"
          @click="handleUpdate"
        >编辑</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >批量导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="批次号"
        align="center"
        prop="batchNo"
        width="120px"
        fixed="left"
      />
      <el-table-column
        label="年度组号"
        align="center"
        prop="yearGroup"
        width="160px"
      />
      <el-table-column
        label="检查时间"
        align="center"
        prop="createTime"
        width="200px"
      >
        <template slot-scope="scope">
          <span
            >{{ scope.row.checkStartDate }} 至
            {{ scope.row.checkEndDate }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="检查事项"
        align="center"
        prop="applyReason"
        width="200px"
      />
      <el-table-column
        label="检查企业"
        align="center"
        prop="vendorName"
        width="300px"
      />
      <el-table-column
        label="检查地点"
        align="center"
        prop="checkAddress"
        width="300px"
      />
      <el-table-column
        label="检查范围"
        align="center"
        prop="checkRange"
        width="160px"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="状态"
        align="center"
        prop="statusStr"
        width="220px"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="requirementsDate"
        width="140px"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="200px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleExamine(scope.row)"
            v-if="scope.row.status == 0 && user.userId== scope.row.auditUserId"
            v-hasPermi="['collect:tapRecord:reviewSubmit']"
            >提交审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleViewAuth(scope.row)"
            v-if="scope.row.status == 4 && user.userId== scope.row.auditUserId"
            v-hasPermi="['collect:tapRecord:reviewWait']"
            >监管人审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleExamine(scope.row)"
            v-if="scope.row.status == 0"
            v-hasPermi="['collect:tapRecord:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleViewAuth(scope.row)"
            v-if="scope.row.status == 1 && user.userId== scope.row.auditUserId"
            v-hasPermi="['collect:tapRecord:review']"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      title="新增抽调记录"
      :visible.sync="open"
      width="500px"
      append-to-body
      @close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="批次号" prop="batchNo">
          <el-select clearable v-model="form.batchNo" style="width: 100%">
            <el-option
              v-for="item in batchNoList"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            rows="5"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
  <Form
    :row="row"
    @onSuccess="onSuccess"
    @onBack="
      () => {
        this.isForm = false;
      }
    "
    v-else
  />
</template>

<script>
import Form from "./form.vue";
import { storage } from "@/utils/util.js";
import {
  vendorRecordsList,
  vendorRecordsAdd,
  vendorRecordsGetWaitList,
} from "@/api/collect/tapRecord/index";
export default {
  components: {
    Form,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      //行内容
      row: {},
      // 选中数组
      ids: [],
      //用户信息
      user: storage.getStorage("user") || {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 录入汇总信息状态
      isForm: false,
      // 总条数
      total: 0,
      // 岗位表格数据
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        yearGroup: undefined,
        applyReason: undefined,
        vendorName: undefined,
        checkAddress: undefined,
        checkRange: undefined,
        status: undefined,
        creatDate: undefined,
      },
      // 检查事项列表
      declarationCategoryList: [],
      // 状态列表
      statusList: [
        { label: "待审核", value: "0" },
        { label: "已审核", value: "1" },
        { label: "已拒绝", value: "2" },
      ],
      // 表单参数
      form: { batchNo: "", remark: "" },
      // 批次号列表
      batchNoList: [],
      // 表单校验
      rules: {
        batchNo: [
          { required: true, message: "批次号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getVendorRecordsGetWaitList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      let params = { ...this.queryParams };
      if (params.creatDate) {
        params.startDate = params.creatDate[0];
        params.endDate = params.creatDate[1];
        delete params.checkDate;
      }
      vendorRecordsList(params).then((response) => {
        this.tableData = response.rows || [];
        this.total = response?.total || 0;
        this.loading = false;
      });
    },

    /** 查询待抽调记录的企业列表 */
    getVendorRecordsGetWaitList() {
      vendorRecordsGetWaitList({}).then((response) => {
        this.batchNoList = response.data || [];
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 提交表单 */
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          vendorRecordsAdd(this.form).then(() => {
            this.Message("新增成功");
            this.open = false;
            this.row = this.form;
            this.isForm = true;
          });
        }
      });
    },
    /** 提交审核 */
    handleExamine(row) {
      row.isAuth = false;
      row.isView = false;
      this.row = row;
      this.isForm = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.postId);
      this.row = selection[0];
    },

    /** 查看 */
    handleView(row) {
      row.isAuth = false;
      row.isView = true;
      this.row = row;
      this.isForm = true;
    },

    /** 新增 */
    handleAdd() {
      this.open = true;
      this.reset();
    },
    /** 编辑 */
    handleUpdate() {
      if (this.ids.length === 0) {
        this.$message.warning("请选择要修改的数据");
        return;
      }
      if (this.ids.length > 1) {
        this.$message.warning("修改只能选择一条数据");
        return;
      }
      this.isForm = true;
    },

    /** 删除 */
    handleDelete() {
      if (this.ids.length === 0) {
        this.$message.warning("请选择要删除的数据");
        return;
      }
      const dictIds = this.row.dictId || this.ids;
      this.$modal
        .confirm('是否确认删除文件夹"' + dictIds + '"的数据项？')
        .then(function () {
          return delType(dictIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "vendor/sampling/records/export",
        {
          ...this.queryParams,
        },
        `抽调记录${new Date().getTime()}.xlsx`,
        {
          headers: { "Content-Type": "application/json;charset=utf-8" },
          isTansParams: true,
        }
      );
    },
    /** 操作审核 */
    handleViewAuth(row) {
      row.isAuth = true;
      row.isView = false;
      this.row = row;
      this.isForm = true;
    },

    /** 成功回调 */
    onSuccess() {
      this.isForm = false;
      this.getList();
    },
  },
};
</script>
